﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace LoL_Profiler
{
    public partial class frmStats : Form
    {
        public frmStats()
        {
            InitializeComponent();
        }

        private void frmStats_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem listItem = new ListViewItem();
            double wins = 0;
            double losses = 0;
            int kills = 0;
            int deaths = 0;
            double assists = 0;
            List<string> summNames1 = new List<string>();
            List<string> summNames2 = new List<string>();

            XmlDocument database = new XmlDocument();
            database.Load(Application.StartupPath + "\\Data.xml");

            // Make a list with people you played more than 2 times with, and remove the duplicates in that list. Might be a better way to do this though.
            # region Name list creator
            XmlNodeList summNames = database.GetElementsByTagName("summonername");

            for (int i = 0; i < summNames.Count; ++i)
            {
                summNames1.Add(summNames[i].InnerText);
                summNames2.Add(summNames[i].InnerText);
            }
            summNames1 = summNames1.Distinct().ToList();
            foreach (string name in summNames1)
            {
                summNames2.Remove(name);
            }
            summNames2 = summNames2.Distinct().ToList();
            #endregion

            #region Calculate wins/losses summoner
            XmlNodeList outcome = database.GetElementsByTagName("outcome");

            for (int i = 0; i < outcome.Count; ++i)
            {
                if (outcome[i].InnerText == "true")
                {
                    wins += 1;
                }
                else
                {
                    losses += 1;
                }
            }
            #endregion

            #region Calculate kills/deaths/assists
            XmlNodeList killsList = database.GetElementsByTagName("kills");
            XmlNodeList deathsList = database.GetElementsByTagName("deaths");
            XmlNodeList assistsList = database.GetElementsByTagName("assists");
            for (int i = 0; i < killsList.Count; ++i)
            {
                kills += Convert.ToInt32(killsList[i].InnerText);
            }
            for (int i = 0; i < deathsList.Count; ++i)
            {
                deaths += Convert.ToInt32(deathsList[i].InnerText);
            }
            for (int i = 0; i < assistsList.Count; ++i)
            {
                assists += Convert.ToInt32(assistsList[i].InnerText);
            }
            #endregion

            #region Add items to listview
            XmlNodeList matches =  database.ChildNodes[1].ChildNodes[0].ChildNodes[2].ChildNodes;
                       


            listItem = new ListViewItem();
            listItem.Text = database.ChildNodes[1].ChildNodes[0].ChildNodes[0].InnerText;
            listItem.SubItems.Add(wins.ToString());
            listItem.SubItems.Add(losses.ToString());
            // calculate percentage
            listItem.SubItems.Add(((wins / (wins + losses)) * 100) + "%");
            listItem.SubItems.Add(String.Format("{0}/{1}/{2}", kills, deaths, assists));
            // Score based on kills, deaths and assists and that divided by the amount of games played
            listItem.SubItems.Add(((kills + (assists * 0.5) - deaths)/(wins+losses)).ToString());
            listView1.Items.Add(listItem);

            int index = 0;
            foreach (string name in summNames2)
            {                
                listItem = new ListViewItem();
                listItem.Text = name;
                listItem.SubItems.Add(winsTeam[index].ToString());
                listItem.SubItems.Add(lossesTeam[index].ToString());
                index += 1;
                listView1.Items.Add(listItem);
            }
            #endregion
        }
    }
}
